package com.xx.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xx.entity.Employees;
import com.xx.exception.BusinessException;
import com.xx.mapper.EmployeesMapper;
import com.xx.service.IEmployeesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;


@Service
public class EmployeesServiceImpl implements IEmployeesService{
    @Autowired
    EmployeesMapper employeesMapper;

    @Override
    public List<Employees> selectAllByEmpNameAndPhoneNumber(String empName, String phoneNumber) {
        return employeesMapper.selectAllByEmpNameAndPhoneNumber("","");
    }

//    获取用户详情通过ID
    @Override
    public Employees selectById(Integer employeeId) {
        return employeesMapper.selectByEmployeeId(employeeId);
    }

    //新增
    @Override
    public int insertBatch(Employees employees) {
        return employeesMapper.insertBatch(Arrays.asList(employees));
    }

    //分页
    @Override
    public PageInfo<Employees> getPage(String empName, String phoneNumber, Integer pageNum) {
        PageHelper.startPage(pageNum,3);
        List<Employees> employeesList =employeesMapper.selectAllByEmpNameAndPhoneNumber(empName, phoneNumber);
        PageInfo<Employees>pageInfo =new PageInfo<>(employeesList);
        return pageInfo;
    }

    //修改
    @Override
    public int updateSelective(Employees employees) {
        return employeesMapper.updateSelective(employees);
    }

    //删除
    @Override
    public int delByEmployeeId(Integer employeeId) {
        return employeesMapper.delByEmployeeId(employeeId);
    }

    //登录
    @Override
    public Employees selectAllByLoginNameAndPassword(String loginName, String password) {
                Employees employees = employeesMapper.selectAllByLoginName(loginName);
        if (employees==null){
            throw new BusinessException(5001,"输入的账号不存在");
        }
        if (!employees.getPassword().equals(password)){
            throw new BusinessException(5002,"密码错误");
        }
        return employees;
    }
}
